package com.example.orderassembly.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.orderassembly.controller.StorePurchaseDetailOrderController;
import com.woniu.cloud.common.dto.Result;
import com.woniuxy.entity.ScoreOrder;
import com.woniuxy.entity.StorePurchaseDetailOrder;
import com.woniuxy.entity.StorePurchaseOrder;
import com.example.orderassembly.mapper.StorePurchaseOrderMapper;
import com.example.orderassembly.service.IStorePurchaseOrderService;
import com.woniuxy.form.GetAfterSaleByPlatform;
import com.woniuxy.form.GetOrder;
import com.woniuxy.form.InsertStorePurchaseDetailOrder;
import com.woniuxy.form.InsertStorePurchaseOrder;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;

/**
 * <p>
 * 店铺进货订单表 服务实现类
 * </p>
 *
 * @author yang
 * @since 2021-09-21
 */
@Service
@SuppressWarnings("all")
public class StorePurchaseOrderServiceImpl extends ServiceImpl<StorePurchaseOrderMapper, StorePurchaseOrder> implements IStorePurchaseOrderService {
    @Resource
    StorePurchaseOrderMapper storePurchaseOrderMapper;
    @Resource
    StorePurchaseDetailOrderController storePurchaseDetailOrderController;

    @Override
    public Boolean insertStorePurchaseOrder(InsertStorePurchaseOrder insertStorePurchaseOrder) {
        System.err.println(insertStorePurchaseOrder);
        StorePurchaseOrder storePurchaseOrder = new StorePurchaseOrder();
        BeanUtils.copyProperties(insertStorePurchaseOrder,storePurchaseOrder);
        // todo 使用Jwt 获取storeId
        Integer storeId = insertStorePurchaseOrder.getStorePurchaseOrderStoreId();
        storePurchaseOrder.setStorePurchaseOrderStoreId(storeId);
        int insert = storePurchaseOrderMapper.insert(storePurchaseOrder);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i = 0; i < insertStorePurchaseOrder.getGoodIdList().size(); i++) {
            map.put(insertStorePurchaseOrder.getGoodIdList().get(i),insertStorePurchaseOrder.getGoodNum().get(i));
        }
        InsertStorePurchaseDetailOrder insertStorePurchaseDetailOrder = new InsertStorePurchaseDetailOrder();
        insertStorePurchaseDetailOrder.setServiceAttribute(map);
        insertStorePurchaseDetailOrder.setStorePurchaseOrder(storePurchaseOrder.getStorePurchaseOrderId());
        insertStorePurchaseDetailOrder.setUserId(storePurchaseOrder.getStorePurchaseOrderStoreId());
        storePurchaseDetailOrderController.insertStorePurchaseDetailOrder(insertStorePurchaseDetailOrder);
        return insert==1;

    }

    @Override
    public IPage<StorePurchaseOrder> getStorePurchaseOrderByStoreId(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        // todo 通过jwt 获取店铺Id
        Integer storeId = getAfterSaleByPlatform.getStoreId();
        Page<StorePurchaseOrder> scoreOrderPage = new Page<>(getAfterSaleByPlatform.getIndexPage(),getAfterSaleByPlatform.getPageSize());
        QueryWrapper<StorePurchaseOrder> scoreOrderQueryWrapper = new QueryWrapper<>();
        scoreOrderQueryWrapper.eq("store_purchase_order_id",storeId);
        scoreOrderQueryWrapper.eq("store_purchase_order_status",getAfterSaleByPlatform.getStatus());
        scoreOrderQueryWrapper.between(null!=getAfterSaleByPlatform.getStartDate()&&null!=getAfterSaleByPlatform.getEndDate(),"store_purchase_order_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
        IPage<StorePurchaseOrder> scoreOrderIPage = storePurchaseOrderMapper.selectPage(scoreOrderPage, scoreOrderQueryWrapper);
        return scoreOrderIPage;
    }

    @Override
    public IPage<StorePurchaseOrder> getStorePurchaseOrderByPlatform(GetAfterSaleByPlatform getAfterSaleByPlatform) {
        Page<StorePurchaseOrder> scoreOrderPage = new Page<>(getAfterSaleByPlatform.getIndexPage(),getAfterSaleByPlatform.getPageSize());
        QueryWrapper<StorePurchaseOrder> scoreOrderQueryWrapper = new QueryWrapper<>();
        scoreOrderQueryWrapper.eq("store_purchase_order_status",getAfterSaleByPlatform.getStatus());
        scoreOrderQueryWrapper.between(null!=getAfterSaleByPlatform.getStartDate()&&null!=getAfterSaleByPlatform.getEndDate(),"store_purchase_order_time",getAfterSaleByPlatform.getStartDate(),getAfterSaleByPlatform.getEndDate());
        IPage<StorePurchaseOrder> scoreOrderIPage = storePurchaseOrderMapper.selectPage(scoreOrderPage, scoreOrderQueryWrapper);
        return scoreOrderIPage;
    }

    @Override
    public Boolean updateStorePurchaseOrderByPlatform(Integer storePurchaseOrderId, Integer status) {
        StorePurchaseOrder storePurchaseOrder = storePurchaseOrderMapper.selectById(storePurchaseOrderId);
        if(storePurchaseOrder.getStorePurchaseOrderStatus()==0&&status==1){
            UpdateWrapper<StorePurchaseOrder> storePurchaseOrderUpdateWrapper = new UpdateWrapper<>();
            storePurchaseOrderUpdateWrapper.set("store_purchase_order_status",status);
            storePurchaseOrderUpdateWrapper.eq("store_purchase_order_id",storePurchaseOrderId);
            int update = storePurchaseOrderMapper.update(null, storePurchaseOrderUpdateWrapper);
            return update==1;
        }
        return false;
    }

    @Override
    public StorePurchaseOrder getStorePurchaseOrderById(Integer id) {
        StorePurchaseOrder storePurchaseOrder = getBaseMapper().selectById(id);
        return storePurchaseOrder;
    }
}
